package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.LsArticleContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LsLawyerArticleMapper;
import com.ruoyi.system.service.ILsLawyerArticleService;

/**
 * 文章Service业务层处理
 *
 * @author zhouyipeng
 * @date 2023-10-16
 */
@Service
public class LsLawyerArticleServiceImpl implements ILsLawyerArticleService
{
    @Autowired
    private LsLawyerArticleMapper lsLawyerArticleMapper;

    @Autowired
    private LsArticleContentMapper lsArticleContentMapper;

    /**
     * 查询文章
     *
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public LsLawyerArticle selectLsLawyerArticleById(String id)
    {
        LsLawyerArticle lsLawyerArticle = lsLawyerArticleMapper.selectLsLawyerArticleById(id);

        if (lsLawyerArticle != null){
            LsArticleContent lsArticleContent = lsArticleContentMapper.selectLsArticleContentById(id);
            if (lsArticleContent != null && StringUtils.isNotEmpty(lsArticleContent.getContent())){
                lsLawyerArticle.setContent(lsArticleContent.getContent());
            }
        }
        return lsLawyerArticle;
    }

    /**
     * 查询文章列表
     *
     * @param lsLawyerArticle 文章
     * @return 文章
     */
    @Override
    public List<LsLawyerArticle> selectLsLawyerArticleList(LsLawyerArticle lsLawyerArticle)
    {
        return lsLawyerArticleMapper.selectLsLawyerArticleList(lsLawyerArticle);
    }

    /**
     * 新增文章
     *
     * @param lsLawyerArticleContent 文章
     * @return 结果
     */
    @Override
    public int insertLsLawyerArticle(LsLawyerArticleContent lsLawyerArticleContent)
    {
        //lsLawyerArticle.setCreateTime(DateUtils.getNowDate());
        //创建id
        String id = IdUtils.randomUUID();
        //对数据进行分析入库
        LsLawyerArticle lsLawyerArticle = new LsLawyerArticle();
        lsLawyerArticle.setCreateTime(lsLawyerArticleContent.getCreateTime());
        lsLawyerArticle.setType(lsLawyerArticleContent.getType());
        lsLawyerArticle.setTitle(lsLawyerArticleContent.getTitle());
        lsLawyerArticle.setId(id);
        lsLawyerArticle.setIsDel(lsLawyerArticleContent.getIsDel());
        //内容数据保存
        LsArticleContent lsArticleContent = new LsArticleContent();
        lsArticleContent.setId(id);
        lsArticleContent.setContent(lsLawyerArticleContent.getContent());
        //保存文章相关数据
        lsLawyerArticleMapper.insertLsLawyerArticle(lsLawyerArticle);
        return lsArticleContentMapper.insertLsArticleContent(lsArticleContent);
    }

    /**
     * 修改文章
     *
     * @param lsLawyerArticleContent 文章
     * @return 结果
     */
    @Override
    public int updateLsLawyerArticle(LsLawyerArticleContent lsLawyerArticleContent)
    {
        LsLawyerArticle lsLawyerArticle = new LsLawyerArticle();
        lsLawyerArticle.setCreateTime(lsLawyerArticleContent.getCreateTime());
        lsLawyerArticle.setType(lsLawyerArticleContent.getType());
        lsLawyerArticle.setTitle(lsLawyerArticleContent.getTitle());
        lsLawyerArticle.setId(lsLawyerArticleContent.getId());
        lsLawyerArticle.setIsDel(lsLawyerArticleContent.getIsDel());
        //内容数据保存
        LsArticleContent lsArticleContent = new LsArticleContent();
        lsArticleContent.setId(lsLawyerArticleContent.getId());
        lsArticleContent.setContent(lsLawyerArticleContent.getContent());
        //修改数据
        lsLawyerArticleMapper.updateLsLawyerArticle(lsLawyerArticle);
        return lsArticleContentMapper.updateLsArticleContent(lsArticleContent);
    }

    /**
     * 批量删除文章
     *
     * @param ids 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteLsLawyerArticleByIds(String[] ids)
    {
        return lsLawyerArticleMapper.deleteLsLawyerArticleByIds(ids);
    }

    /**
     * 删除文章信息
     *
     * @param id 文章主键
     * @return 结果
     */
    @Override
    public int deleteLsLawyerArticleById(String id)
    {
        return lsLawyerArticleMapper.deleteLsLawyerArticleById(id);
    }

    /**
     *
     * @Description 获取文章每组五条数据
     * @author wangxf
     * @date 2023/10/17 10:24
     * @param
     * @return java.util.List<com.ruoyi.system.domain.LsLawyerArts>
     * @Version 1.0
     **/
    @Override
    public List<LsLawyerArts> queryLawyerArticle() {
        //查询每组最新的五条数据
        List<LsLawyerArticle> articleList = lsLawyerArticleMapper.queryLawyerArticle();
        //对数据进行封装成想要的数据
        List<LsLawyerArts> resultList = new ArrayList<>();
        //对数据按照类型分组封装
        Map<Integer, List<LsLawyerArticle>> mapGroup = articleList.stream().collect(Collectors.groupingBy(LsLawyerArticle::getType));
        mapGroup.forEach((key,value)->{
            LsLawyerArts lsLawyerArts = new LsLawyerArts();
            lsLawyerArts.setName(ArticleType.values()[key]);
            lsLawyerArts.setType(key);
            lsLawyerArts.setLsLawyerArtList(value);
            resultList.add(lsLawyerArts);
        });
        return resultList;
    }
}

